package cn.yunhe.test;

import cn.yunhe.entity.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;

public class Demo2 {
    SessionFactory sessionFactory=null;
    Session session = null;
    Transaction tr = null;
    @Before
    public void init(){
        Configuration configuration = new Configuration().configure();
        sessionFactory =configuration.buildSessionFactory();
        session = sessionFactory.openSession();
        tr = session.beginTransaction();
    }
    @Test
    public void testManyToOne(){
        JieDao jieDao = (JieDao) session.get(JieDao.class,1);
        System.out.println("街道名："+jieDao.getJdname()+" , 区县的名字："+jieDao.getQuXian().getQxname());
    }
    @Test
    public void testOneToMany(){
        QuXian quXian= (QuXian) session.get(QuXian.class,1);
        System.out.println("区县是："+quXian.getQxname()+"下面的街道有：");

        Iterator<JieDao> jds = quXian.getJds().iterator();
        if(!jds.hasNext()){
            System.out.println("这是一个空的");
        }
        while (jds.hasNext()){
            System.out.println(jds.next().getJdname());
        }
    }
    @Test
    public void testAddManytoOne(){//添加多到一
        JieDao jieDao1 =new JieDao();
        jieDao1.setJdname("东大街12");

        JieDao jieDao2 =new JieDao();
        jieDao2.setJdname("西大街12");

        JieDao jieDao3 =new JieDao();
        jieDao3.setJdname("商城路12");

        session.save(jieDao1);
        session.save(jieDao2);
        session.save(jieDao3);
        QuXian quXian = new QuXian();
        quXian.setQxname("管城区1");
        quXian.getJds().add(jieDao1);
        quXian.getJds().add(jieDao2);
        quXian.getJds().add(jieDao3);

        session.save(quXian);
    }
    @Test
    public void delMany(){//删除多的表中个
        JieDao jieDao = (JieDao) session.get(JieDao.class,19);
        QuXian quXian = (QuXian) session.get(QuXian.class,7);
        System.out.println(quXian);
        quXian.getJds().remove(jieDao);
        System.out.println(quXian);
        session.delete(jieDao);
    }
    @Test
    public void delOne(){//删除一的表中
        QuXian quXian = (QuXian) session.get(QuXian.class,6);
        System.out.println(quXian);
        session.delete(quXian);

    }
    @Test
    public void testManyToMany(){
        Course java =  new Course();
        Course php =  new Course();
        java.setCoursename("java");
        php.setCoursename("php");
        session.save(java);
        session.save(php);

        Student stu1 = new Student();
        Student stu2 = new Student();
        stu1.setStudentname("小军");
        stu2.setStudentname("小丽");

        stu1.getCourses().add(java);
        stu1.getCourses().add(php);
        stu2.getCourses().add(java);
        stu2.getCourses().add(php);

        session.save(stu1);
        session.save(stu2);
    }

    @After
    public void destory(){
        tr.commit();
        session.close();
    }
}
